package cn.iocoder.springboot.lab17.dynamicdatasource.controller;

import cn.iocoder.springboot.lab17.dynamicdatasource.service.CommonService;
import cn.iocoder.springboot.lab17.dynamicdatasource.util.MinioUtils;
import io.minio.http.Method;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.ZonedDateTime;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Description minio文件上传控制器
 * @Author 孙率众
 * @Date 2024/04/17
 */
@CrossOrigin
@RestController
@RequestMapping("/api")
@Slf4j
public class MinioFileUploadController {

    @Autowired
    CommonService commonService;
    @Autowired
    private MinioUtils minioUtils;

    /**
     * @param bucketName 桶名称
     * @return {@link Object }
     * @Description 判断bucket是否存在，不存在则创建
     * @Author 孙率众
     * @Date 2024/04/17
     */
    @GetMapping("/existBucket")
    public Object existBucket(String bucketName) {
        minioUtils.existBucket(bucketName);
        return "成功";
    }

    /**
     * @param bucketName 桶名称
     * @return {@link Object }
     * @Description 删除bucket
     * @Author 孙率众
     * @Date 2024/04/17
     */
    @GetMapping("/deleteBucket")
    public Object deleteBucket(String bucketName) {
        minioUtils.removeBucket(bucketName);
        return "删除成功";
    }

    /**
     * @param bucketName 存储bucket名称
     * @param fileName   文件名称
     * @return {@link Object }
     * @Description 获取上传临时签名
     * @Author 孙率众
     * @Date 2024/04/17
     */
    @GetMapping("/getPolicy")
    public Object getPolicy(String bucketName, String fileName) {
        return minioUtils.getPolicy(bucketName, fileName, ZonedDateTime.now());
    }

    /**
     * @param bucketName 存储bucket名称
     * @param fileName   文件名称
     * @return {@link Object }
     * @Description 获取上传文件的url
     * @Author 孙率众
     * @Date 2024/04/17
     */
    @GetMapping("/getPolicyUrl")
    public Object getPolicyUrl(String bucketName, String fileName) {
        return minioUtils.getPolicyUrl(bucketName, fileName, Method.GET, 30000, TimeUnit.MILLISECONDS);
    }

    /**
     * @param file     文件
     * @param fileName 文件名称
     * @return {@link Object }
     * @Description 上传文件
     * @Author 孙率众
     * @Date 2024/04/17
     */
    @GetMapping("/upload")
    public Object uploadFile(@RequestParam("file") MultipartFile file, String fileName, String bucketName) throws IOException {
        minioUtils.upload(bucketName, file, fileName);
        return "上传成功";
    }

    /**
     * @param fileName 文件名称
     * @return {@link ResponseEntity }
     * @Description dowload文件
     * @Author 孙率众
     * @Date 2024/04/17
     */
    @GetMapping("/dowload")
    public ResponseEntity dowloadFile(@RequestParam("fileName") String fileName, String bucketName) {
        return minioUtils.download(bucketName, fileName);
    }

    /**
     * @param fileName 文件名称
     * @return {@link Object }
     * @Description 得到文件url
     * @Author 孙率众
     * @Date 2024/04/17
     */
    @GetMapping("/getUrl")
    public Object getFileUrl(@RequestParam("fileName") String fileName, String bucketName) {
        return minioUtils.getFileUrl(bucketName, fileName);
    }

    @GetMapping("/copy")
    public void copy(@RequestParam String tableName,
                     @RequestParam String keyField,
                     @RequestParam(required = false) String oldFilePathField,
                     @RequestParam(required = false) String newFilePathField,
                     @RequestParam(required = false) String bucketName) {
        try {
            commonService.copy(tableName, keyField, oldFilePathField, newFilePathField, bucketName);
        } catch (Exception e) {
            log.info("迁移发生异常", e);
        }
    }
}

